package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.dao.IChannelDao;
import com.magicode.cms.dao.IVideoDao;
import com.magicode.cms.entiy.base.Video;
import com.magicode.cms.entiy.base.WebsiteComment;
import com.magicode.cms.service.IWebsiteCommentService;
import com.magicode.portal.dao.IWebsiteCommentDao;

@Service("websiteCommentService")
@Transactional
public class WebsiteCommentServiceImpl extends BaseServiceImpl<WebsiteComment, IWebsiteCommentDao> implements IWebsiteCommentService {
	
	@Autowired
	@Qualifier("daoWebsiteComment")
	private IWebsiteCommentDao websiteCommentDao;
	public IWebsiteCommentDao getDao() throws Exception {
		return websiteCommentDao;
	}

	@Override
	public void addWebsiteComment(WebsiteComment websiteComment, String userid) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Map<String, Object>> getWebsiteComment() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deleteAll(String ids) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "author");
		map.put(json_title,"作者");
		map.put("width", 80);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "contactInfo");
		map.put(json_title,"邮箱或手机号");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "textString");
		map.put(json_title, "留言内容");
		map.put("width", 360);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "createTime");
		map.put(json_title, "留言时间");
		map.put("width", 120);
		beaninfo.add(map);
//		map = new HashMap<String, Object>();
//		map.put(json_key, "perate");
//		map.put(json_title, "删除");
//		map.put("width", 70);
//		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql ="from " +WebsiteComment.class.getName()+" "+entity_alias_name;
		hql += " left join "+entity_alias_name+".author as au ";
		if(null!=map && !map.isEmpty()){
			hql += " where 1 = 1 ";
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+map.get(field_id)+"'";
			}
		}
		return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		List<Map<String, Object>> resultList = null;
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			hql += " order by t.createTime desc ";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("author", "au.loginName as author ");
			gridsel.put("contactInfo", ""+entity_alias_name+".contactInfo as contactInfo ");
			gridsel.put("textString", ""+entity_alias_name+".text as textString ");
			gridsel.put("createTime", ""+entity_alias_name+".createTime as createTime ");
			resultList=this.queryMapDataList(hql, gridsel, start, size);
			resultList=super.convertByteToString(resultList, "textString");
			return resultList;
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}

}
